package com.flute.tools.sql;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import com.flute.tools.util.StringUtil;

public class FieldValuesSQL implements ISQL,Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4890847426921847082L;
	private LinkedHashSet<SQLCondition> fieldMap;
	private String spliter = ", ";

	public FieldValuesSQL() {
		this.fieldMap = new LinkedHashSet<SQLCondition>();
	}
	
	public FieldValuesSQL(String spliter) {
		this.fieldMap = new LinkedHashSet<SQLCondition>();
		this.spliter = spliter;
	}

	public void addFieldValue(String field, String value) {
		this.fieldMap.add(new SQLCondition(field, value));
	}
	
	public void addSQLCondition(SQLCondition condition){
		this.fieldMap.add(condition);
	}

	public int size() {
		return fieldMap.size();
	}

	@Override
	public String getSQLString() {
		if (fieldMap.size() == 0) {
			return "";
		} else {
			List<String> list = new ArrayList<String>();
			for (SQLCondition con : fieldMap) {
				list.add(con.getSQLString());
			}
			return SQLUtils.BLANK
					+ StringUtil.getStringFromStrings(list
							.toArray(new String[] {}), spliter)
					+ SQLUtils.BLANK;
		}
	}
}
